import { handPickingOrderApi } from "@/api/waybillManagement/stockManagement";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formState, emit, loading, tableData, formDataRef, getWarehouseOpts } = state;
  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      // const params = tableData.value.filter((t: any) => {
      //   return t.shipmentBoxNumber && t.id;
      // });
      const find = getWarehouseOpts.value.find((t: any) => t.id === formState.warehouseId) || {};
      const params = {
        warehouseName: find.cnName,
        warehouseId: formState.warehouseId,
        bizPreOrderShipmentList: tableData.value.filter((t: any) => {
          return t.shipmentBoxNumber && t.id;
        })
      };

      if (!params.bizPreOrderShipmentList.length) {
        loading.value = false;
        return message.error("至少一个箱子!");
      }
      loading.value = true;
      handPickingOrderApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  const cancelHandle = () => {
    emit("update:visible", false);
  };
  return {
    submitHandle,
    cancelHandle
  };
}
